package ca.ericslandry.client.mvp.presenter;

import java.util.List;
import java.util.Set;

import net.customware.gwt.dispatch.client.DispatchAsync;
import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.Place;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import ca.ericslandry.client.GwtCallback;
import ca.ericslandry.client.resource.MyConstants;
import ca.ericslandry.shared.domain.Issue;
import ca.ericslandry.shared.rpc.GetIssuesResult;
import ca.ericslandry.shared.rpc.GetMyIssues;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.inject.Inject;

public class PlacesPresenter extends BasePresenter<PlacesPresenter.Display> {
	public interface Display extends BaseDisplay {
		/**
		 * Shows grid of geohashes
		 */
		void reset();

		Set<String> getPreferredGeohash();

		void showIssue(Long id);

		void showIssues(List<Issue> issues);

		void showGrid();
	}

	public static final Place PLACE = new Place("places");
	@SuppressWarnings("unused")
	private ContentPresenter contentPresenter;
	private DispatchAsync dispatcher;

	@Inject
	public PlacesPresenter(final Display display, final EventBus eventBus, ContentPresenter contentPresenter,
			DispatchAsync dispatcher) {
		super(display, eventBus);
		this.contentPresenter = contentPresenter;
		this.dispatcher = dispatcher;
		bind();
	}

	@Override
	protected void onBind() {
		Log.debug("onBind: places");
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}

	@Override
	protected void onPlaceRequest(PlaceRequest placeReq) {
		String action = placeReq.getParameter(ACTION, null);
		if (VIEW.equalsIgnoreCase(action)) {
			Long id = Long.parseLong(placeReq.getParameter(ID, "0"));
			if (id != 0) {
				Log.info("onPlaceRequest: show issue " + id + " on map");
				display.showIssue(id);
			}
		} else if (MY.equalsIgnoreCase(action)) {
			Log.info("onPlaceRequest: show my issues");
			showMyIssues();
		} else if (BROWSE.equalsIgnoreCase(action)) {
			Log.info("onPlaceRequest: browse issues");
			display.showGrid();
		}
	}

	private void showMyIssues() {
		dispatcher.execute(new GetMyIssues(), new GwtCallback<GetIssuesResult>(dispatcher, eventBus, display) {
			@Override
			public void callback(GetIssuesResult result) {
				List<Issue> issues = result.getIssueList();
				display.showIssues(issues);
			}
		});
	}

	public static PlaceRequest getDefaultPlaceRequest() {
		return new PlaceRequest(PLACE).with(ACTION, MY);
	}

	public static Hyperlink getLinkMyLocation() {
		PlaceRequest placeReq = new PlaceRequest(PLACE).with(ACTION, MY);
		return new Hyperlink(MyConstants.INSTANCE.myLocations(), placeReq.toString());
	}

	public static Hyperlink getLinkBrowseLocations() {
		PlaceRequest placeReq = new PlaceRequest(PLACE).with(ACTION, BROWSE);
		return new Hyperlink(MyConstants.INSTANCE.browseLocations(), placeReq.toString());
	}
}
